package com.dynamicarray;

/**
 * Created by 27931 on 2020/7/18.
 */
public class ArrayList {

    private int size;
    private int[] elements;
    private static final int DEFAULT_CAPACITY = 10;
    private static final int ELEMENT_NOT_FOUND = -1;

    public ArrayList(int capacity) {
        capacity = (capacity < DEFAULT_CAPACITY) ? DEFAULT_CAPACITY : capacity;
        elements = new int[capacity];
    }

    public ArrayList() {
        this(DEFAULT_CAPACITY);
    }

    /**
     * 元素的数量
     *
     * @return
     */
    public int size() {
        return size;
    }

    /**
     * 查看元素的位置
     *
     * @param element
     * @return
     */
    public int indexOf(int element) {
        for (int i = 0; i < size; i++) {
            if (elements[i] == element) return i;
        }
        return ELEMENT_NOT_FOUND;
    }

    /**
     * 是否包含某个元素
     *
     * @param element
     * @return
     */
    boolean contains(int element) {
        return indexOf(element) != ELEMENT_NOT_FOUND;
    }

    /**
     * 是否为空
     *
     * @return
     */
    public boolean isEmpty() {
        return size == 0;
    }

    /**
     * 返回index位置对应的元素
     *
     * @param index
     * @return
     */
    public int get(int index) {
        RangeCheck(index);
        return elements[index];
    }

    /**
     * 添加元素到最后面
     *
     * @param element
     */
    public void add(int element) {
        add(size,element);
    }

    /**
     * 往index位置添加元素
     * @param index
     * @param element
     */
    public void add(int index, int element){
        RangeCheckForAdd(index);
        for (int i = size-1; i >=index ; i--) {
            elements[i+1]=elements[i];
        }
        elements[index]=element;
        size++;
    }


    /**
     * 删除index位置对应的元素*
     *
     * @param index
     * @return
     */
    public int remove(int index) {
        RangeCheck(index);
        int old = elements[index];
        for (int i = index + 1; i <= size - 1; i++) {
            elements[i - 1] = elements[i];
        }
        size--;
        return old;
    }

    /**
     * 设置index位置的元素
     *
     * @param index
     * @param element
     * @return
     */
    public int set(int index, int element) {
        RangeCheck(index);
        int old = elements[index];
        elements[index] = element;
        return old;
    }

    /**
     * 清除所有元素
     */
    public void clear() {
        size = 0;
    }

    private void OutOfBound(int index){
        throw new IndexOutOfBoundsException("index=" + index + ",size=" + size);
    }
    private void RangeCheck(int index){
        if (index < 0 || index >= size) {
            OutOfBound(index);
        }
    }
    private void RangeCheckForAdd(int index){
        if (index < 0 || index > size) {
            OutOfBound(index);
        }
    }
    @Override
    public String toString() {
        StringBuilder stringBuilder = new StringBuilder();
        stringBuilder.append("size=").append(size).append(",[");
        for (int i = 0; i < size; i++) {
            if (i != 0) {
                stringBuilder.append(", ");
            }
            stringBuilder.append(elements[i]);
        }
        stringBuilder.append("]");
        return stringBuilder.toString();
    }
}
